package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.aiservice.StarRocksAIService;
import org.example.service.DatabaseService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/test")
public class TestController {

    private final StarRocksAIService starRocksAIService;
    private final DatabaseService databaseService;

    public TestController(StarRocksAIService starRocksAIService,
                          DatabaseService databaseService) {
        this.starRocksAIService = starRocksAIService;
        this.databaseService = databaseService;
    }

    @GetMapping("/sql-generation")
    public String testSqlGeneration() {
        try {
            String metadata = databaseService.getSimplifiedDatabaseMetadata();
            String sql = starRocksAIService.generateSQL("显示各产品类别销售额占比", metadata);
            return "生成的SQL: " + sql;
        } catch (Exception e) {
            return "测试失败: " + e.getMessage();
        }
    }

    @GetMapping("/database-test")
    public String testDatabase() {
        try {
            List<String> tables = databaseService.getTables();
            return "数据库正常，表数量: " + tables.size();
        } catch (Exception e) {
            return "数据库测试失败: " + e.getMessage();
        }
    }

    @GetMapping("/health")
    public String health() {
        return "服务运行正常";
    }
}